package com.ruoyi.web.exams.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.exams.domain.ExamPager;
import com.ruoyi.web.exams.service.IExamPagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 试卷Controller
 * 
 * @author ruoyi
 * @date 2024-10-02
 */
@Api("试卷Controller")

@RestController
@RequestMapping("/exam/pager")
public class ExamPagerController extends BaseController
{
    @Autowired
    private IExamPagerService examPagerService;

    /**
     * 查询试卷列表
     */
    @ApiOperation("查询试卷列表")

    @PreAuthorize("@ss.hasPermi('exam:pager:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExamPager examPager)
    {
        startPage();
        List<ExamPager> list = examPagerService.selectExamPagerList(examPager);
        return getDataTable(list);
    }

    /**
     * 导出试卷列表
     */
    @ApiOperation("导出试卷列表")

    @PreAuthorize("@ss.hasPermi('exam:pager:export')")
    @Log(title = "试卷", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamPager examPager)
    {
        List<ExamPager> list = examPagerService.selectExamPagerList(examPager);
        ExcelUtil<ExamPager> util = new ExcelUtil<ExamPager>(ExamPager.class);
        util.exportExcel(response, list, "试卷数据");
    }

    /**
     * 获取试卷详细信息
     */
    @ApiOperation("获取试卷详细信息")

    @PreAuthorize("@ss.hasPermi('exam:pager:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(examPagerService.selectExamPagerById(id));
    }

    /**
     * 新增试卷
     */
    @ApiOperation("新增试卷")

    @PreAuthorize("@ss.hasPermi('exam:pager:add')")
    @Log(title = "试卷", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExamPager examPager)
    {
        return toAjax(examPagerService.insertExamPager(examPager));
    }

    /**
     * 修改试卷
     */
    @ApiOperation("修改试卷")

    @PreAuthorize("@ss.hasPermi('exam:pager:edit')")
    @Log(title = "试卷", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExamPager examPager)
    {
        return toAjax(examPagerService.updateExamPager(examPager));
    }

    /**
     * 删除试卷
     */
    @ApiOperation("删除试卷")

    @PreAuthorize("@ss.hasPermi('exam:pager:remove')")
    @Log(title = "试卷", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(examPagerService.deleteExamPagerByIds(ids));
    }

    /**
     * 开始答题,传入试卷ID,返回试卷情况及其题目列表
     * @param pagerId
     * @return
     */
    @ApiOperation("开始答题,传入试卷ID,返回试卷情况及其题目列表")

    @PreAuthorize("@ss.hasPermi('exam:result:add')")
    @Log(title = "开始答题", businessType = BusinessType.INSERT)
    @GetMapping("/startPager/{pagerId}")
    public AjaxResult startPager(@PathVariable Long pagerId)
    {
        return AjaxResult.success(examPagerService.startPager(pagerId));
    }
}
